Anaphoric Macro
   HOME

TheInfoList



OR:

An anaphoric macro is a type of programming macro that deliberately captures some form supplied to the macro which may be referred to by an ''anaphor'' (an expression referring to another). Anaphoric macros first appeared in Paul Graham's '' On Lisp''Chapter 6
of Let over Lambda
and their name is a reference to linguistic anaphora—the use of words as a substitute for preceding words.


Examples

The loop macro in
ANSI Common Lisp Common Lisp (CL) is a dialect of the Lisp (programming language), Lisp programming language, published in American National Standards Institute, ANSI standard document ''ANSI INCITS 226-1994 (S20018)'' (formerly ''X3.226-1994 (R1999)''). The Comm ...
is anaphoric in binding, where the it expression refers to the result of the test expression in a clause. Here is an example that sums the value of non-nil elements, where it refers to the values of elements that do not equal nil: (loop for element in '(nil 1 nil 2 nil nil 3 4 6) when element sum it) ;; ⇒ 16 Here it is bound to the output of (and (> number 3) number) when true, collecting numbers larger than 3:6.1.8.1 Examples of clause grouping
from the
Common Lisp HyperSpec The Common Lisp HyperSpec is a technical standard document written in the hypertext format ''Hypertext Markup Language'' (HTML). It is not the American National Standards Institute The American National Standards Institute (ANSI ) is a priv ...
(loop for number from 1 to 6 when (and (> number 3) number) collect it) ; IT refers to (and (> number 3) number). ;; ⇒ (4 5 6)


Defining anaphoric macros

One example is an anaphoric version of the if-then-else construct, which introduces an ''anaphor'' it, bound to the result of the test clause:Chapter 14. Anaphoric Macros
of On Lisp by Paul Graham
(defmacro aif (test-form then-form &optional else-form) `(let ((it ,test-form)) (if it ,then-form ,else-form))) (aif (+ 2 7) (format nil "~A does not equal NIL." it) (format nil "~A does equal NIL." it)) ;; ⇒ "9 does not equal NIL." Another example is an anaphoric version of the λ-function, which binds the function itself to the ''anaphor'' self, allowing it to recur: (defmacro alambda (parms &body body) `(labels ((self ,parms ,@body)) #'self)) ;; Factorial function defined recursively where `self' refers to the alambda function (alambda (n) (if (= n 0) 1 (* n (self (1- n)))))


See also

*
Anonymous recursion In computer science, anonymous recursion is recursion which does not explicitly call a function by name. This can be done either explicitly, by using a higher-order function – passing in a function as an argument and calling it – or implicitly ...
*
Hygienic macro Hygienic macros are macros whose expansion is guaranteed not to cause the accidental capture of identifiers. They are a feature of programming languages such as Scheme, Dylan, Rust, Nim, and Julia. The general problem of accidental capture was w ...
s *
Macro (computer science) In computer programming, a macro (short for "macro instruction"; ) is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion. The input and output ...
*
Method chaining Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement ...
*
this (computer programming) this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity of which the currently running code is a part. The entity referred to by these keywords thus depends on the execution con ...


References


External links

{{wikibooks, Common Lisp, External libraries/Extended Binding#Anaphoric macros with Anaphora, Anaphoric macros with Anaphora
Chapter 14. Anaphoric Macros
from On Lisp by Paul Graham
Anaphora
— an anaphoric macro collection Lisp (programming language) Programming constructs Source code Articles with example Lisp (programming language) code